package com.yiwei.jmx.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.IconSetBean;
import com.yiwei.jmx.api.bean.UploadImgBean;
import com.yiwei.jmx.api.bean.UserSexBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.util.Dialog;
import com.yiwei.jmx.util.ImageUtil;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.V19FrameLayout;
import com.yiwei.jmx.view.uploadImage.UploadImgAdapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class PersonalCenterActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.btn_icon)
    LinearLayout btnIcon;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.btn_phone)
    LinearLayout btnPhone;
    @BindView(R.id.txt_nickname)
    TextView txtNickname;
    @BindView(R.id.btn_nickname)
    LinearLayout btnNickname;
    @BindView(R.id.txt_sex)
    TextView txtSex;
    @BindView(R.id.btn_sex)
    LinearLayout btnSex;
    @BindView(R.id.txt_invite)
    TextView txtInvite;
    @BindView(R.id.btn_invite)
    LinearLayout btnInvite;
    @BindView(R.id.txt_comment)
    TextView txtComment;
    @BindView(R.id.btn_comment)
    LinearLayout btnComment;
    @BindView(R.id.btn_qrcode)
    LinearLayout btnQrcode;
    private Unbinder unbiner;

    private UploadImgAdapter uploadImgAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("个人资料");
        txtPhone.setText(UserInfoGlobal.getSomeMobile());

        txtSex.setText(UserInfoGlobal.getUserSex());
        txtInvite.setText(UserInfoGlobal.getInviteCode());
        Glide.with(this).load(UserInfoGlobal.getUserIcon()).error(R.mipmap.img_default_icon).into(imgIcon);
    }


    @Override
    protected void onResume() {
        super.onResume();
        txtNickname.setText(UserInfoGlobal.getUserNick());
    }


    public void getUserSex(String sex) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        UserSexBean item = new UserSexBean();
        item.sex = sex;

        loadingDialog.show();
        Api.api_service.getUserSex(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    txtSex.setText(sex.equals("1") ? "男" : "女");
                    UserInfoGlobal.saveUserSex(sex);
                    ToastUtil.makeText(this, "修改成功");
                });
    }


    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImage(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {

            }
        }, 1);
    }

    public void getUploadImage(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        UploadImgBean bean = new UploadImgBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.getUploadImage(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    loadingDialog.dismiss();
                    getSetIcon(result.url);
                });
    }

    public void getSetIcon(String url) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        IconSetBean bean = new IconSetBean();
        bean.photo = url;
        Api.api_service.getIconSet(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    Glide.with(this).load(url).into(imgIcon);
                    UserInfoGlobal.setUserIcon(url);
                });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_comment, R.id.btn_qrcode, R.id.btn_back, R.id.btn_icon, R.id.btn_phone, R.id.btn_sex, R.id.txt_nickname})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_comment:

                if (UserInfoGlobal.getIsHasInvite()) {
                    Util.startActivity(this, MyRefereeActivity.class);
                } else {
                    Util.startActivity(this, MySetRefereeActivity.class);
                }
                break;
            case R.id.btn_qrcode:
                Util.startActivity(this, MyQrCodeSecActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;

            case R.id.txt_nickname:
                if (!UserInfoGlobal.hasUserNick()) {
                    Util.startActivity(this, NicknameActivity.class);
                } else {
                    ToastUtil.makeText(this, "昵称只能设置一次");
                }
                break;
            case R.id.btn_icon:
                checkPermission(new CheckPermListener() {
                    @Override
                    public void superPermission() {
                        if (uploadImgAdapter == null) {
                            initUploadImgAdapter();
                        }
                        uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
                    }

                    @Override
                    public void onReject() {

                    }
                }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);

                break;
            case R.id.btn_phone:
//                Util.startActivity(this, ChangePhoneTotalActivity.class);
                break;
            case R.id.btn_sex:
                Dialog.showDefaultDialog(this, "", "选择性别", "男", "女", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        getUserSex("2");
                    }

                    @Override
                    public void cancel() {
                        getUserSex("1");
                    }
                });
                break;


        }
    }

}
